package com.jike.noobmoney.util;

import android.util.Log;

import java.security.MessageDigest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class SignUtil {
    public static void main(String[] args) {
        try {
            // 假如返回的是这些
            String callbackParams = "advert_id=1500002895&advert_name=%E4%B9%90%E4%BA%AB%E6%B8%B8&content=%E4%B9%90%E4%BA%AB%E6%B8%B8%E7%99%BB%E9%99%86%E6%B8%B8%E6%88%8F%E8%AF%95%E7%8E%A93%E5%88%86%E9%92%9F%E8%8E%B7%E5%BE%970.55%E5%85%83&created=1564640415&device_id=99001215466228&media_id=dy_dy_59610925&media_income=0.44&member_income=0.55&order_id=127786693&user_id=100000&sign=56390d2f431e6d65acd77fbc4095e713";
            // 实际上是这样获取
            // String callbackParams = request.getQueryString();
            // 第一步：回调的是这些参数转化成map
            Map<String, String> demoMap = getParameter(callbackParams);
            // 第二步：生成签名，第二个参数只是测试用的appSeret，务必换成你的appSecret
            String localSign = generateSignature(demoMap, "7892d05cdec7f54715d6ab85ed37bf88");
            System.out.println("localSign = " + localSign);
            // 第三部：校验签名
            String sign = demoMap.get("sign");
            if (sign.equalsIgnoreCase(localSign)) {
                System.out.println("验签成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Map<String, String> getParameter(String params) {
        Map<String, String> map = new HashMap<String, String>();
        try {
            final String charset = "utf-8";
            String[] keyValues = params.split("&");
            for (int i = 0; i < keyValues.length; i++) {
                String key = keyValues[i].substring(0, keyValues[i].indexOf("="));
                String value = keyValues[i].substring(keyValues[i].indexOf("=") + 1);
                map.put(key, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
    /**
     * 
     * data : 参数map key：appSecret
     * 
     */
    public static String generateSignature(final Map<String, String> data, String key){
        Set<String> keySet = data.keySet();
        String[] keyArray = keySet.toArray(new String[keySet.size()]);
        Arrays.sort(keyArray);
        StringBuilder sb = new StringBuilder();
        for (String k : keyArray) {
            // 如果参数中有sign,不参与生成签名，直接跳过
            if (k.equals("sign")) {
                continue;
            }
            sb.append(k).append("=").append(data.get(k)).append("&");
        }
        sb.append("key=").append(key);
        Log.e("msg",sb.toString());
//
//        Log.e("后台的msg",MD5(sb.toString()).toLowerCase());
//        Log.i("自己的msg",MD5.GetMD5Code(sb.toString(),true));

        return MD5(sb.toString()).toLowerCase();
    }
    public static String MD5(String data) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] array = md.digest(data.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder();
            for (byte item : array) {
                sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
            }
            return sb.toString().toLowerCase();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }
}